﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Planning.Classes;

namespace Planning.Forms {
    public partial class choixFormationForm : Form {
        #region Constrcut
        private planningForm parentForm;

        public choixFormationForm(DateTime date, planningForm parent) {
            InitializeComponent();
            // Stocke la fenêtre parente
            this.parentForm = parent;
            // Transforme la date en format date-heure
            String debut = date.ToString("yyyy-MM-dd 00:00:00:000");
            String fin = date.ToString("yyyy-MM-dd 23:59:59:999");

            // Récupère les formations du jour
            DataTable formations = SQL.requete("SELECT id_formation, libelle_formation FROM formation WHERE debut_formation BETWEEN CONVERT(datetime, '" + debut + "', 21) " + "AND CONVERT(datetime, '" + fin + "',21);");
            // Lie la ComboBox à la DataTable
            this.formationList.DataSource = formations;
            this.formationList.DisplayMember = "libelle_formation";
            this.formationList.ValueMember = "id_formation";
            // Combo box non modifiable
            this.formationList.DropDownStyle = ComboBoxStyle.DropDownList;
        }
        #endregion

        #region clics
        private void button2_Click(object sender, EventArgs e) {
            // Ferme la fenêtre
            this.Dispose();
        }

        private void choixFormation_Click(object sender, EventArgs e) {
            // Si admin
            if (this.parentForm.isAdmin) {
                // Ouvre le formulaire de modification de formation
                ajoutFormationForm form = new ajoutFormationForm(Convert.ToInt32(this.formationList.SelectedValue), this.parentForm);
                form.Show();
            }
            // Si visiteur
            else if(this.parentForm.isVisiteur) {
                inscriptionFormationForm form = new inscriptionFormationForm(Convert.ToInt32(this.formationList.SelectedValue));
                form.Show();
            }
            // Si utilisateur
            else {
                // Ouvre le formulaire d'inscription à la formation
                inscriptionFormationForm form = new inscriptionFormationForm(Convert.ToInt32(this.formationList.SelectedValue), this.parentForm.infoConnexion);
                form.Show();
            }
            // Ferme la fenêtre
            this.Dispose();

        }
        #endregion
    }
}
